import React from 'react';
import { getBaseUrl } from '@/services/urlconst';
import { requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { message } from 'antd';
/**
 * 行政区域更新业务逻辑
 * @param permissions
 */
const AreaNumUpdateService = () => {
  /** 表格数据请求处理业务 */
  const { loading, run } = useRequest(
    (record: Record<string, any>) => {
      const addURL = `${getBaseUrl()}/areaNumController/add`;
      const editURL = `${getBaseUrl()}/areaNumController/edit`;
      let params = {
        ...record,
      };
      if (record?.id) {
        return requestFunComponFiex('PUT', editURL, params);
      } else {
        return requestFunComponFiex('POST', addURL, params);
      }
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        if (res && res?.msgFlag) {
          message.success(res?.msg);
        } else {
          message.error(res?.msg);
        }
      },
    },
  );

  /** 请求提交数据 */
  const updateRequest = React.useCallback(
    (requestParams: Record<string, any>) => {
      run({
        ...requestParams,
      });
    },
    [],
  );

  return {
    loading,
    updateRequest,
  };
};

export { AreaNumUpdateService };
